package com.atguigu.jxc.controller;

import com.alibaba.fastjson.JSON;
import com.atguigu.jxc.domain.ErrorCode;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.DamageList;
import com.atguigu.jxc.entity.DamageListGoods;
import com.atguigu.jxc.service.DamageListService;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: lei
 * @Date 2021/11/26 19:52
 **/
@RestController
public class DamageController {

    @Autowired
    private DamageListService damageListService;

    /**
     * 新增报损单
     * 请求URL：http://localhost:8080/damageListGoods/save?damageNumber=BS1605766644460（报损单号,前端生成）
     * 请求参数：DamageList damageList, String damageListGoodsStr
     * 请求方式：POST
     * 返回值类型：JSON
     * 返回值：ServiceVO
     */
    @PostMapping("damageListGoods/save")
    public ServiceVO save(HttpServletRequest request){
        try {
            String damageListGoodsStr = request.getParameter("damageListGoodsStr");
            List<DamageListGoods> damageListGoodsList = JSON.parseArray(damageListGoodsStr, DamageListGoods.class);

            // 获取damageList
            DamageList damageList = new DamageList();
            String damageDate = request.getParameter("damageDate");
            String remarks = request.getParameter("remarks");
            String damageNumber = request.getParameter("damageNumber");
            damageList.setDamageDate(damageDate);
            damageList.setDamageNumber(damageNumber);
            damageList.setRemarks(remarks);

            damageListService.save(damageList,damageListGoodsList);

            return new ServiceVO(SuccessCode.SUCCESS_CODE,SuccessCode.SUCCESS_MESS,null);
        } catch (Exception e) {
            e.printStackTrace();
            return new ServiceVO(ErrorCode.REQ_ERROR_CODE,ErrorCode.REQ_ERROR_MESS,null);
        }
    }


    /**
     * 报损单查询
     * 请求URL：http://localhost:8080/damageListGoods/list
     * 请求参数：String  sTime（开始时间）, String  eTime（结束时间）
     * 请求方式：POST
     * 返回值类型：JSON
     * 返回值：Map<String,Object>
     */
    @PostMapping("damageListGoods/list")
    public Map<String,Object> listDamageList(HttpServletRequest request){
        String sTime = request.getParameter("sTime");
        String eTime = request.getParameter("eTime");

        Map map = damageListService.listDamageList(sTime,eTime);
        return map;
    }

    /**
     * 查询报损单商品信息
     * 请求URL：http://localhost:8080/damageListGoods/goodsList
     * 请求参数：Integer damageListId（报损单Id）
     * 请求方式：POST
     * 返回值类型：JSON
     * 返回值：Map<String,Object>
     * Response Example Value：
     * {
     *     "rows": [
     */
    @PostMapping("damageListGoods/goodsList")
    public Map<String,Object> goodsList(HttpServletRequest request){
        String damageListId = request.getParameter("damageListId");
        Map map = damageListService.selectGoodsList(damageListId);
        return  map;
    }


}
